package com.econ.powercloud.ems.service.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.exception.CommonRuntimeException;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.common.base.facade.util.JacksonUtil;
import com.econ.powercloud.ems.dal.entity.EmsClusterConfig;
import com.econ.powercloud.ems.dal.entity.EmsClusterDetailConfig;
import com.econ.powercloud.ems.dal.entity.EmsClusterModelConfig;
import com.econ.powercloud.ems.pojo.EmsClusterDetailConfigVO;
import com.econ.powercloud.ems.pojo.EmsClusterModelConfigVO;
import com.econ.powercloud.ems.pojo.requestVO.EmsClusterModelEdit;
import com.econ.powercloud.ems.pojo.requestVO.EmsClusterModelRequestVO;
import com.econ.powercloud.ems.service.dao.EmsClusterConfigDAO;
import com.econ.powercloud.ems.service.dao.EmsClusterDetailConfigDAO;
import com.econ.powercloud.ems.service.dao.EmsClusterModelConfigDAO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description : 电池簇模型service
 * @Date : 2022/5/26 16:46
 * @Modified by : zjf
 */
@Service
public class ClusterModelService {

    @Autowired
    private EmsClusterModelConfigDAO emsClusterModelConfigDAO;
    @Autowired
    private EmsClusterDetailConfigDAO emsClusterDetailConfigDAO;
    @Autowired
    private EmsClusterConfigDAO emsClusterConfigDAO;

    public PageResult<List<EmsClusterModelConfigVO>> selectByCondition(String modelName, Integer pageNo, Integer pageSize){
        Page<EmsClusterModelConfig> page = emsClusterModelConfigDAO.selectByModelName(modelName,pageNo,pageSize);
        if (page.getRecords().size() > 0) {
            List<EmsClusterModelConfigVO> modelConfigVOList = JacksonUtil.convertToList(page.getRecords(), EmsClusterModelConfigVO.class);
            return PageResult.<List<EmsClusterModelConfigVO>>builder().data(modelConfigVOList).pageNo(pageNo).totalCount((int) page.getTotal()).build();
        }
        return PageResult.<List<EmsClusterModelConfigVO>>builder().data(Collections.EMPTY_LIST).pageNo(pageNo).totalCount((int) page.getTotal()).build();
    }

    /**
     * 通过主键删除电池簇模型
     * @param id
     * @return
     */
    public void deleteById(Integer id){
        List<EmsClusterConfig> clusterConfigList = emsClusterConfigDAO.selectByType(id);
        if(CollectionUtils.isNotEmpty(clusterConfigList)){
            throw new CommonRuntimeException(StatusType.FAIL,"该模型正在被使用,无法删除！");
        }
        emsClusterModelConfigDAO.removeById(id);
        emsClusterDetailConfigDAO.deleteByType(id);
    }

    public String addClusterModel(EmsClusterModelRequestVO requestVO){
        List<EmsClusterModelConfig> modelConfigList = emsClusterModelConfigDAO.list();
        List<String> modelNames = modelConfigList.stream().map(EmsClusterModelConfig::getModelName).collect(Collectors.toList());
        if(modelNames.contains(requestVO.getModelName())){
            throw new CommonRuntimeException(StatusType.FAIL,"电池簇模型名称重复!");
        }
        EmsClusterModelConfig emsClusterModelConfig = EmsClusterModelConfig.builder()
                .modelName(requestVO.getModelName()).packNum(requestVO.getPackNum())
                .gmtCreate(System.currentTimeMillis()).gmtModify(System.currentTimeMillis())
                .build();
        emsClusterModelConfigDAO.save(emsClusterModelConfig);
        List<EmsClusterDetailConfig> emsClusterDetailConfigs = Lists.newArrayList();
        requestVO.getPackInfos().stream().forEach(d->{
            emsClusterDetailConfigs.add(EmsClusterDetailConfig.builder()
                    .type(emsClusterModelConfig.getId()).pack(d.getPack())
                    .batteriesNum(d.getBatteriesNum()).temperNum(d.getTemperNum())
                    .build());
        });
        emsClusterDetailConfigDAO.saveBatch(emsClusterDetailConfigs);
        return requestVO.getModelName();
    }

    public EmsClusterModelConfigVO selectByModelId(Integer id){
        EmsClusterModelConfig emsClusterModelConfig = emsClusterModelConfigDAO.getById(id);
        Assert.notNull(emsClusterModelConfig,"该电池簇模型不存在!");
        List<EmsClusterDetailConfig> emsClusterDetailConfigs = emsClusterDetailConfigDAO.getListByType(emsClusterModelConfig.getId());
        EmsClusterModelConfigVO emsClusterModelConfigVO = JacksonUtil.convertToObj(emsClusterModelConfig, EmsClusterModelConfigVO.class);
        List<EmsClusterDetailConfigVO> configVOS = JacksonUtil.convertToList(emsClusterDetailConfigs, EmsClusterDetailConfigVO.class);
        emsClusterModelConfigVO.setPackInfos(configVOS);
        return emsClusterModelConfigVO;
    }

    public void updateClusterPackInfo(EmsClusterModelEdit emsClusterModelEdit){
        EmsClusterModelConfig emsClusterModelConfig = emsClusterModelConfigDAO.getById(emsClusterModelEdit.getId());
        Assert.notNull(emsClusterModelConfig,"该电池簇模型不存在!");
        emsClusterModelConfig.setModelName(emsClusterModelEdit.getModelName());
        emsClusterModelConfig.setGmtModify(System.currentTimeMillis());
        emsClusterModelConfigDAO.updateById(emsClusterModelConfig);
        emsClusterDetailConfigDAO.deleteByType(emsClusterModelConfig.getId());
        List<EmsClusterDetailConfig> detailConfigs = JacksonUtil.convertToList(emsClusterModelEdit.getPackInfos(), EmsClusterDetailConfig.class);
        detailConfigs.stream().forEach(d-> d.setType(emsClusterModelConfig.getId()));
        emsClusterDetailConfigDAO.saveBatch(detailConfigs);
    }
}
